package com.cet.algorithm.二叉树.对称二叉树;

import java.util.*;

/**
 * @program: algorithm
 * @description: 对称二叉树判断
 * @author: 陈恩涛
 * @create: 2022-07-19 11:53
 **/
public class LC101 {

    public static void main(String[] args) {
        final TreeNode root2 = new TreeNode(1, new TreeNode(2, null, new TreeNode(3)), new TreeNode(2, null, new TreeNode(3)));
        final TreeNode root = new TreeNode(1, new TreeNode(2, new TreeNode(3), new TreeNode(4)), new TreeNode(2, new TreeNode(4), new TreeNode(3)));
        System.out.println(isSymmetric(root));
        System.out.println(isSymmetric2(root));

        System.out.println(isSymmetric(root2));
        System.out.println(isSymmetric2(root2));
    }

    public static boolean isSymmetric(TreeNode root) {
        // return compare(root.left, root.right);
        final List<Integer> traversal1 = inorderTraversal(root);
        final List<Integer> traversal2 = inorderTraversal2(root);
        final int size1 = traversal1.size();
        final int size2 = traversal2.size();
        if (size1 != size2) {
            return false;
        }
        for (int i = 0; i < size1; i++) {
            if (!traversal1.get(i).equals(traversal2.get(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 使用栈解决
     * @param root 根节点
     * @return 结果
     */
    public static boolean isSymmetric2(TreeNode root) {
        final Deque<TreeNode> deque = new LinkedList<>();
        deque.offerLast(root.left);
        deque.offerFirst(root.right);
        while (!deque.isEmpty()) {
            final TreeNode left = deque.pollLast();
            final TreeNode right = deque.pollFirst();
            if (left == null && right == null) {
                continue;
            }
            if (left == null || right == null || left.val != right.val) {
                return false;
            }
            deque.offerLast(left.right);
            deque.offerLast(left.left);
            deque.offerFirst(right.left);
            deque.offerFirst(right.right);
        }
        return true;
    }

    /**
     * 递归法
     * @param left
     * @param right
     * @return
     */
    private boolean compare(TreeNode left, TreeNode right) {

        if (left == null && right != null) {
            return false;
        }
        if (left != null && right == null) {
            return false;
        }
        if (left == null && right == null) {
            return true;
        }
        if (left.val != right.val) {
            return false;
        }
        // 比较外侧
        final boolean compareOutside = compare(left.left, right.right);
        // 比较内侧
        final boolean compareInside = compare(left.right, right.left);
        return compareInside && compareOutside;
    }

    /**
     * 通用遍历法，中序遍历 ltr
     * @param root 根节点
     * @return 结果
     */
    public static List<Integer> inorderTraversal(TreeNode root) {
        if (root == null) {
            return null;
        }
        final Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        final List<Integer> result = new ArrayList<>();
        while (!stack.isEmpty()) {
            TreeNode cur = stack.peek();
            if (cur != null) {
                stack.pop();
                if (cur.right != null) {
                    stack.push(cur.right);
                }
                stack.push(cur);
                stack.push(null);
                if (cur.left != null) {
                    stack.push(cur.left);
                }
            } else {
                stack.pop();
                cur = stack.pop();
                result.add(cur.val);
            }
        }
        return result;
    }

    /**
     * 通用遍历法，中序遍历 rtl
     * @param root 根节点
     * @return 结果
     */
    public static List<Integer> inorderTraversal2(TreeNode root) {
        if (root == null) {
            return null;
        }
        final Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        final List<Integer> result = new ArrayList<>();
        while (!stack.isEmpty()) {
            TreeNode cur = stack.peek();
            if (cur != null) {
                stack.pop();
                if (cur.left != null) {
                    stack.push(cur.left);
                }
                stack.push(cur);
                stack.push(null);

                if (cur.right != null) {
                    stack.push(cur.right);
                }
            } else {
                stack.pop();
                cur = stack.pop();
                result.add(cur.val);
            }
        }
        return result;
    }
}
